package com.woniuxy.bookmallmanagersrever.controller;

import com.github.pagehelper.PageInfo;
import com.woniuxy.bookmallmanagersrever.entity.TUrlPermission;
import com.woniuxy.bookmallmanagersrever.service.PermissionService;
import com.woniuxy.bookmallmanagersrever.utils.JwtUtils;
import com.woniuxy.bookmallmanagersrever.vo.PermissionVo;
import com.woniuxy.bookmallmanagersrever.vo.ResponseResult;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.awt.print.Printable;
import java.sql.ResultSet;
import java.util.List;

@RestController
public class PermissionController {

    @Autowired
    PermissionService permissionService ;

    @Autowired
    JwtUtils  jwtUtils ;

    @GetMapping("/permission/list")
    public ResponseResult<PageInfo<TUrlPermission>> list(
            @RequestParam(value = "now",defaultValue = "1") Integer now,
            @RequestParam(value = "size",defaultValue = "3") Integer size,
            String name){
        ResponseResult<PageInfo<TUrlPermission>> result = new ResponseResult<>();
        try {
            PageInfo<TUrlPermission> lists = permissionService.findPermissionByPage(now,size,name);
            result.setCode(200);
            result.setMsg("查询成功!");
            result.setData(lists);
        }catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("查询失败!");
        }
        return result ;

    }

    @PostMapping("/permission/add")
    public ResponseResult<Void> add(@RequestBody TUrlPermission permission){
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            permissionService.add(permission);
            result.setCode(200);
            result.setMsg("添加成功!");
        }catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("添加失败!");
        }
        return result ;
    }

    @PutMapping("/permission/modify")
    public ResponseResult<Void> modify(@RequestBody TUrlPermission permission){
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            permissionService.modify(permission);
            result.setCode(200);
            result.setMsg("修改成功!");
        }catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("修改失败!");
        }
        return result ;
    }
    //删除
    @DeleteMapping("/permission/del/{id}")
    public ResponseResult<Void> modify(@PathVariable("id") int id){
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            permissionService.removeByKey(id);
            result.setCode(200);
            result.setMsg("删除成功!");
        }catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("删除失败!");
        }
        return result ;
    }



    @GetMapping("/permission/loadAllMenus")
    public ResponseResult<List<PermissionVo>> loadAllMenus(){
        ResponseResult<List<PermissionVo>> result = new ResponseResult<>();
        try {
            List<PermissionVo> lists = permissionService.findAllMenus();
            result.setCode(200);
            result.setMsg("查询成功!");
            result.setData(lists);
        }catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("查询失败!");
        }
        return result ;
    }

    @GetMapping("/permission/getAllParent")
    public ResponseResult<List<TUrlPermission>> getAllParent(){
        ResponseResult<List<TUrlPermission>> result = new ResponseResult<>();
        try {
            List<TUrlPermission> lists = permissionService.findAllParentPermission();
            result.setCode(200);
            result.setMsg("查询成功!");
            result.setData(lists);
        }catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("查询失败!");
        }
        return result ;
    }

    @GetMapping("/permission/loadMenusByManager")
    public ResponseResult<List<PermissionVo>> loadMenusByManager(HttpServletRequest request){
        ResponseResult<List<PermissionVo>> result = new ResponseResult<>();
        try {
            String token = request.getHeader("token");
            String username = jwtUtils.getUsername(token);
            long id = jwtUtils.getId(token);
            List<PermissionVo> lists = null;
            if ("admin".equals(username)) {
                lists = permissionService.findAllMenus();
            }else {
                lists = permissionService.findMenusByManager(username);
            }
            result.setCode(200);
            result.setMsg("查询成功!");
            result.setData(lists);
        }catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("查询失败!");
        }
        return result ;
    }
}
